package com.og.ouroa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.exception.Assert;
import com.og.ouroa.common.result.ResponseEnum;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.mapper.UserInfoMapper;
import com.og.ouroa.pojo.vo.LoginVO;
import com.og.ouroa.pojo.vo.RegisterVO;
import com.og.ouroa.pojo.vo.UserInfoVO;
import com.og.ouroa.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.og.ouroa.utils.JwtUtils;
import com.og.ouroa.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {


    public void register(RegisterVO registerVO){

        //判断手机号是否被注册
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",registerVO.getMobile());
        Integer count = baseMapper.selectCount(wrapper);
        Assert.isTrue(count==0, ResponseEnum.MOBILE_EXIST_ERROR);

        UserInfo userInfo = new UserInfo();

        //插入数据
        BeanUtils.copyProperties(registerVO,userInfo);
        userInfo.setPassword(MD5Utils.encrypt(userInfo.getPassword())); //密码加密

        baseMapper.insert(userInfo);

    }

    @Override
    public UserInfoVO login(LoginVO loginVO) {
       //根据vo获取用户
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",loginVO.getNumber());
        queryWrapper.eq("identity",loginVO.getIdentity());
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);



        //判断用户是否存在
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);


        //校验密码

        Assert.equals(MD5Utils.encrypt(loginVO.getPassword()),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);


        //用户是否被禁用
        Assert.isTrue(userInfo.getStatus(),ResponseEnum.LOGIN_LOKED_ERROR);

        //校验通过  对象复制
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userInfo,userInfoVO);


        //生成token
        String token = JwtUtils.createToken(userInfo.getNumber(),userInfo.getName(),userInfo.getIdentity());
        userInfoVO.setToken(token);

        return userInfoVO;
    }

    @Override
    public void imputUrlbyId(Integer sid, String url) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",sid);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);
        Assert.notNull(userInfo,ResponseEnum.ABNORMAL_ACCOUNT);
        userInfo.setImg(url);
        baseMapper.updateById(userInfo);

    }

    @Override
    public void updatePwd(String password, Integer sid) {
     baseMapper.updatepwd(MD5Utils.encrypt(password),sid);
    }

    @Override
    public IPage<UserInfo> listTeacher(Page<UserInfo> userInfoPage) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",1);
        return baseMapper.selectPage(userInfoPage,queryWrapper);
    }

    @Override
    public Boolean checkNumber(Boolean identity, String num) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identity",identity)
        .eq("number",num);
        Integer count = baseMapper.selectCount(queryWrapper);
        System.out.println(count);
        Assert.isTrue(count==0, ResponseEnum.USER_EXISTS);

        return true;
    }

    @Override
    public Integer getidByNumber(String number) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",number);
         UserInfo userInfo = baseMapper.selectOne(queryWrapper);
         return userInfo.getId();
    }


}
